package com.fhsk.file.common.utils.file;

import com.fhsk.file.common.enums.WatermarkEnum;
import com.fhsk.file.common.enums.WatermarkFontEnum;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;

/**
 * 水印工具类
 */
@Slf4j
public class WatermarkUtil {

    public void addWaterMark() throws Exception {
        String srcFile = "D:\\work\\pdf\\win10.pdf";//要添加水印的文件
        String text = "系统集成公司";//要添加水印的内容
        int textWidth = 200;
        int textHeight = 440;
        PdfReader reader = new PdfReader(srcFile);// 待加水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(new File("D:\\work\\pdf\\addWaterMark.pdf")));// 加完水印的文件
//          byte[] userPassword = "123".getBytes();
        byte[] ownerPassword = "12345".getBytes();
//          int permissions = PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_MODIFY_CONTENTS|PdfWriter.ALLOW_PRINTING;
//          stamper.setEncryption(null, ownerPassword, permissions,false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_ASSEMBLY, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_COPY, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_DEGRADED_PRINTING, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_FILL_IN, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_CONTENTS, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_SCREENREADERS, false);
        stamper.setEncryption(null, ownerPassword, PdfWriter.DO_NOT_ENCRYPT_METADATA, false);
        stamper.setViewerPreferences(PdfWriter.HideToolbar | PdfWriter.HideMenubar);
//          stamper.setViewerPreferences(PdfWriter.HideWindowUI);
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        BaseFont font = BaseFont.createFont("font/SIMKAI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        for (int i = 1; i < total; i++)// 循环对每页插入水印
        {
            content = stamper.getUnderContent(i);// 水印的起始
            content.beginText();// 开始
            content.setColorFill(BaseColor.GREEN);// 设置颜色 默认为蓝色
            content.setFontAndSize(font, 38);// 设置字体及字号
            content.setTextMatrix(textWidth, textHeight);// 设置起始位置
            content.showTextAligned(Element.ALIGN_LEFT, text, textWidth, textHeight, 45);// 开始写入水印
            content.endText();
        }
        stamper.close();
    }

    /**
     * pdf 添加水印方法，返回添加水印后的字节数组输出流
     *
     * @param pdfInputStream
     * @param watermarkUrl
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMark(ByteArrayInputStream pdfInputStream, String watermarkUrl) throws IOException, DocumentException {
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamp = null;
        try {
            reader = new PdfReader(pdfInputStream, "PDF".getBytes());
            stamp = new PdfStamper(reader, nowBos);
            Image img = Image.getInstance(watermarkUrl);// 插入水印
            img.setAbsolutePosition(0, 0);
            int pageSize = reader.getNumberOfPages();
            for (int i = 1; i <= pageSize; i++) {
                PdfContentByte under = stamp.getOverContent(i);
                under.addImage(img);
            }
        } finally {
            if (null != stamp) {
                try {
                    stamp.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * pdf 添加水印方法，返回添加水印后的字节数组输出流
     *
     * @param image
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMark(ByteArrayInputStream inputStream, MultipartFile image) throws IOException, DocumentException {
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            byte[] ownerPassword = String.valueOf(new Date().getTime()).getBytes();
            reader = new PdfReader(inputStream, ownerPassword);
            stamper = new PdfStamper(reader, nowBos);

            stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            Image img = Image.getInstance(image.getBytes());// 插入水印
            img.setAbsolutePosition(0, 0);
            int pageSize = reader.getNumberOfPages();
            for (int i = 1; i <= pageSize; i++) {
                PdfContentByte under = stamper.getOverContent(i);
                under.addImage(img);
            }

        } finally {
            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * pdf 添加水印方法，返回添加水印后的字节数组输出流
     *
     * @param pdfFile
     * @param image
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMark(MultipartFile pdfFile, MultipartFile image) throws IOException, DocumentException {
        ByteArrayInputStream inputStream = null;
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            inputStream = new ByteArrayInputStream(pdfFile.getBytes());
            byte[] ownerPassword = String.valueOf(new Date().getTime()).getBytes();
            reader = new PdfReader(inputStream, ownerPassword);
            stamper = new PdfStamper(reader, nowBos);

            stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            Image img = Image.getInstance(image.getBytes());// 插入水印
            img.setAbsolutePosition(0, 0);
            int pageSize = reader.getNumberOfPages();
            for (int i = 1; i <= pageSize; i++) {
                float pageWidth = reader.getPageSize(i).getWidth();
                float pageHeight = reader.getPageSize(i).getHeight();
                img.scaleAbsolute(pageWidth, pageHeight);

                PdfContentByte under = stamper.getOverContent(i);
                under.addImage(img);
            }

        } finally {
            if (null != inputStream) {
                inputStream.close();
            }

            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * pdf 添加文字水印方法，返回添加水印后的字节数组输出流
     *
     * @param inputStream
     * @param text
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMarkText(ByteArrayInputStream inputStream, String text,
                                                            Integer type, Float opacity, Integer fontColor) throws IOException, DocumentException {
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            byte[] ownerPassword = String.valueOf(new Date().getTime()).getBytes();
            reader = new PdfReader(inputStream, ownerPassword);
            stamper = new PdfStamper(reader, nowBos);

            stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            createWaterMarkText(reader, stamper, text, type, opacity, fontColor);

        } finally {
            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * pdf 添加文字水印方法，返回添加水印后的字节数组输出流
     *
     * @param pdfFile
     * @param text
     * @param type
     * @param opacity
     * @param fontColor
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMarkText(MultipartFile pdfFile, String text, Integer type, Float opacity, Integer fontColor) throws IOException, DocumentException {
        ByteArrayInputStream inputStream = null;
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            inputStream = new ByteArrayInputStream(pdfFile.getBytes());
            byte[] ownerPassword = String.valueOf(new Date().getTime()).getBytes();
            reader = new PdfReader(inputStream, ownerPassword);
            stamper = new PdfStamper(reader, nowBos);

            stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

            createWaterMarkText(reader, stamper, text, type, opacity, fontColor);

        } finally {
            if (null != inputStream) {
                inputStream.close();
            }

            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * 设置基础字体
     *
     * @throws Exception
     */
    private static BaseFont getBaseFont() throws IOException, DocumentException {
        /**
         * pdf基础字体路径
         */
        String prefixFont;
        String os = System.getProperties().getProperty("os.name");
        if (os.startsWith("win") || os.startsWith("Win")) {
            prefixFont = "D:\\file" + File.separator;
        } else {
            prefixFont = "/usr/share/fonts/chinese" + File.separator;
        }

        // 设置基础中文字体为：方正兰亭黑
        BaseFont bfChinese = BaseFont.createFont(prefixFont + "FZCSJW.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

        return bfChinese;
    }

    public static void createWaterMarkText(PdfReader reader, PdfStamper stamper, String text,
                                           Integer type, Float opacity, Integer fontColor) throws IOException, DocumentException {
//        BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        BaseFont base = getBaseFont();

        Rectangle pageRect = null;
        PdfGState gs = new PdfGState();
        // 设置填充操作的不透明度，0.0f表示完全透明，1.0f表示完全不透明，默认不透明度是1.0f
        gs.setFillOpacity(opacity == null ? 0.05f : opacity);
        // 设置描边（stroke）操作的不透明度，0.0f表示完全透明，1.0f表示完全不透明，默认不透明度是1.0f
//        gs.setStrokeOpacity(0.1f);
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte under;
        for (int i = 1; i < total; i++) {
            pageRect = reader.getPageSizeWithRotation(i);
            under = stamper.getOverContent(i);
            under.saveState();
            under.setGState(gs);
            under.beginText();
            under.setFontAndSize(base, 45);
            // 设置字体颜色，默认值：黑色
            if (WatermarkFontEnum.RED.getCode().equals(fontColor)) {
                under.setColorFill(BaseColor.RED);
            } else if (WatermarkFontEnum.ORANGE.getCode().equals(fontColor)) {
                under.setColorFill(BaseColor.ORANGE);
            } else if (WatermarkFontEnum.BLUE.getCode().equals(fontColor)) {
                under.setColorFill(BaseColor.BLUE);
            } else if (WatermarkFontEnum.BLACK.getCode().equals(fontColor)) {
                under.setColorFill(BaseColor.BLACK);
            }

            // 你可以随心所欲的改你自己想要的角度
            // 水印文字成30度角倾斜
            float pageHeight = pageRect.getHeight();
            float pageWidth = pageRect.getWidth();

            if (WatermarkEnum.TEXT.getCode().equals(type)) {
                // 普通文字水印
                int length = text.length();
                if (length <= 3) {
                    setTextOf3w(text, under, pageHeight, pageWidth, 45);
                } else if (4 == length) {
                    setTextOf4w(text, under, pageHeight, pageWidth, 45);
                } else if (5 == length) {
                    setTextOf5w(text, under, pageHeight, pageWidth, 45);
                } else if (6 == length) {
                    setTextOf6w(text, under, pageHeight, pageWidth, 45);
                } else if (7 == length) {
                    setTextOf7w(text, under, pageHeight, pageWidth, 45);
                } else if (8 == length) {
                    setTextOf8w(text, under, pageHeight, pageWidth, 45);
                } else if (9 == length) {
                    setTextOf9w(text, under, pageHeight, pageWidth, 45);
                } else if (10 == length) {
                    setTextOf10w(text, under, pageHeight, pageWidth, 45);
//                } else if (11 == length) {
//                    setTextOf11w(text, under, pageHeight, pageWidth, 45);
                } else {
                    setTextOf12w(text, under, pageHeight, pageWidth, 45);
                }
            } else if (WatermarkEnum.SIMPLE.getCode().equals(type)) {
                // 简单文字水印
                setSimpleText(text, under, pageHeight, pageWidth, 45);
            } else if (WatermarkEnum.LOWER_RIGHT.getCode().equals(type)) {
                // 右下角单独文字水印
                setTextOfLowerRight(text, under, pageHeight, pageWidth, 45);
            }

            // 添加水印文字
            under.endText();
        }
    }

    /**
     * pdf 添加水印方法，返回添加水印后的字节数组输出流
     *
     * @param srcFile
     * @param image
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static ByteArrayOutputStream pdfAddWaterMark(String srcFile, MultipartFile image) throws IOException, DocumentException {
        ByteArrayOutputStream nowBos = new ByteArrayOutputStream();
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {

            byte[] ownerPassword = String.valueOf(new Date().getTime()).getBytes();
            reader = new PdfReader(srcFile, ownerPassword);
            stamper = new PdfStamper(reader, nowBos);

//          int permissions = PdfWriter.ALLOW_COPY|PdfWriter.ALLOW_MODIFY_CONTENTS|PdfWriter.ALLOW_PRINTING;
//          stamper.setEncryption(null, ownerPassword, permissions,false);

            stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);

//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_ASSEMBLY, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_COPY, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_DEGRADED_PRINTING, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_FILL_IN, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_ANNOTATIONS, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_MODIFY_CONTENTS, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.ALLOW_SCREENREADERS, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setEncryption(null, ownerPassword, PdfWriter.DO_NOT_ENCRYPT_METADATA, PdfWriter.ENCRYPTION_AES_128);
//			stamper.setViewerPreferences(PdfWriter.HideToolbar|PdfWriter.HideMenubar);

            Image img = Image.getInstance(image.getBytes());// 插入水印
            img.setAbsolutePosition(0, 0);
            int pageSize = reader.getNumberOfPages();
            for (int i = 1; i <= pageSize; i++) {
                PdfContentByte under = stamper.getOverContent(i);
                under.addImage(img);
            }

        } finally {
            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }

        return nowBos;
    }

    /**
     * 将网络地址pdf文件 转成ByteArrayInputStream
     *
     * @param pdfUrl
     * @return
     */
    public static ByteArrayInputStream pdfUrl2ByteArrayInputStream(String pdfUrl) {
        org.apache.commons.io.output.ByteArrayOutputStream bao = null;
        InputStream in = null;
        ByteArrayInputStream bain = null;
        try {
            URL url = new URL(pdfUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            // 设置超时间为3秒
            conn.setConnectTimeout(3 * 1000);
            // 防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            // 得到输入流
            in = conn.getInputStream();
            byte[] buff = new byte[2048];
            int bytesRead = 0;
            bao = new org.apache.commons.io.output.ByteArrayOutputStream();
            while ((bytesRead = in.read(buff)) != -1) {
                bao.write(buff, 0, bytesRead);
            }
            bao.flush();
//			in.close();
            byte[] data = bao.toByteArray();
            bain = new ByteArrayInputStream(data);
        } catch (Exception e) {
            log.error("读取网络文件异常:" + pdfUrl);
        } finally {
            if (null != bao) {
                try {
                    bao.close();
                } catch (IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }
        }
        return bain;
    }

    /**
     * PDF添加文字水印
     *
     * @param srcFile       源文件(PDF)
     * @param outSrcPath    目标输出文件(PDF)
     * @param watermarktext 水印(文本)
     * @throws IOException
     * @throws DocumentException
     */
    public static void addWatermark(String srcFile, String outSrcPath, String watermarktext) throws IOException, DocumentException {
        // 待加水印的文件
        PdfReader reader = new PdfReader(new FileInputStream(srcFile));

        if (outSrcPath == null || outSrcPath.trim().equals("")) {
            outSrcPath = "D:\\waterMark.pdf";
        }
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outSrcPath));
        // 设置字体
        BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
        // 设置透明度
        PdfGState gs = new PdfGState();
        // pdf页数
        int pageCount = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        // 循环对每页插入水印
        for (int i = 1; i < pageCount; i++) {
            // 水印的起始
            content = stamper.getOverContent(i);
            gs.setFillOpacity(0.5f);
            content.setGState(gs);
            // 开始
            content.beginText();
            // 设置颜色 默认为黑色
            content.setColorFill(BaseColor.LIGHT_GRAY);
            // 设置字体及字号
            content.setFontAndSize(baseFont, 50);
            // 开始写入水印
            content.showTextAligned(Element.ALIGN_BASELINE, watermarktext, 180, 340, 45);
            content.endText();
        }
        stamper.close();
        reader.close();
    }

    /**
     * pdf加水印
     *
     * @param inputFile
     * @param outputFile
     * @param text
     */
    public static void waterMark(String inputFile, String outputFile, String text, Integer type, Float opacity, Integer fontColor) {
        PdfReader reader = null;
        PdfStamper stamper = null;
        try {
            reader = new PdfReader(inputFile);
            System.out.println("读取PDF文件模板完了");
            stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

            createWaterMarkText(reader, stamper, text, type, opacity, fontColor);

            System.out.println("PDF文件水印处理成功");
        } catch (Exception e) {
            System.out.println("流程处理过程出现异常" + e.getMessage());
            log.error("流程处理过程出现异常，请联系管理员！" + e.getMessage());
        } finally {
            if (null != stamper) {
                try {
                    stamper.close();
                } catch (DocumentException | IOException e) {
                    log.error("流关闭异常:" + e.getMessage());
                }
            }

            if (null != reader) {
                reader.close();
            }
        }
    }

    private static void setTextOf3w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 第一排
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 78 / 611f, pageHeight * (868 - 343) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 328 / 611f, pageHeight * (868 - 267) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 494 / 611f, pageHeight * (868 - 276) / 868f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 166 / 611f, pageHeight * (868 - 428) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 333 / 611f, pageHeight * (868 - 436) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 502 / 611f, pageHeight * (868 - 442) / 868f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * -10 / 611f, pageHeight * (868 - 591) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 172 / 611f, pageHeight * (868 - 600) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 342 / 611f, pageHeight * (868 - 606) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 507 / 611f, pageHeight * (868 - 611) / 868f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 10 / 611f, pageHeight * (868 - 760) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 179 / 611f, pageHeight * (868 - 767) / 868f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 346 / 611f, pageHeight * (868 - 772) / 868f, rotation);
    }

    private static void setTextOf4w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 87 / 650f, pageHeight * (910f - 356) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 402 / 650f, pageHeight * (910f - 239) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 230 / 650f, pageHeight * (910f - 408) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 450 / 650f, pageHeight * (910f - 370) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 43 / 650f, pageHeight * (910f - 578) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 239 / 650f, pageHeight * (910f - 585) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 414 / 650f, pageHeight * (910f - 593) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 20 / 650f, pageHeight * (910f - 794) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 196 / 650f, pageHeight * (910f - 795) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 387 / 650f, pageHeight * (910f - 794) / 910f, rotation);
    }

    private static void setTextOf5w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 86 / 650f, pageHeight * (910f - 346) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 311 / 650f, pageHeight * (910f - 353) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 63 / 650f, pageHeight * (910f - 597) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 401 / 650f, pageHeight * (910f - 502) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 144 / 650f, pageHeight * (910f - 762) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 376 / 650f, pageHeight * (910f - 760) / 910f, rotation);
    }

    private static void setTextOf6w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 74 / 650f, pageHeight * (910f - 365) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 322 / 650f, pageHeight * (910f - 351) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 56 / 650f, pageHeight * (910f - 617) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 405 / 650f, pageHeight * (910f - 503) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 136 / 650f, pageHeight * (910f - 773) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 368 / 650f, pageHeight * (910f - 774) / 910f, rotation);
    }

    private static void setTextOf7w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 59 / 650f, pageHeight * (910f - 387) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 343 / 650f, pageHeight * (910f - 339) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 47 / 650f, pageHeight * (910f - 636) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 402 / 650f, pageHeight * (910f - 512) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 104 / 650f, pageHeight * (910f - 807) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 343 / 650f, pageHeight * (910f - 799) / 910f, rotation);
    }

    private static void setTextOf8w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 27 / 650f, pageHeight * (910f - 366) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 351 / 650f, pageHeight * (910f - 291) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 10 / 650f, pageHeight * (910f - 632) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 400 / 650f, pageHeight * (910f - 487) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 33 / 650f, pageHeight * (910f - 859) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 308 / 650f, pageHeight * (910f - 852) / 910f, rotation);
    }

    private static void setTextOf9w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 27 / 650f, pageHeight * (910f - 366) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 391 / 650f, pageHeight * (910f - 266) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 0 / 650f, pageHeight * (910f - 644) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 400 / 650f, pageHeight * (910f - 487) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 20 / 650f, pageHeight * (910f - 871) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 295 / 650f, pageHeight * (910f - 873) / 910f, rotation);
    }

    private static void setTextOf10w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 25 / 650f, pageHeight * (910f - 382) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 441 / 650f, pageHeight * (910f - 343) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 24 / 650f, pageHeight * (910f - 764) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 282 / 650f, pageHeight * (910f - 890) / 910f, rotation);
    }

    private static void setTextOf11w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 25 / 650f, pageHeight * (910f - 382) / 910f, rotation);
//        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 480 / 650f, pageHeight * (910f - 304) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 103 / 650f, pageHeight * (910f - 610) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 282 / 650f, pageHeight * (910f - 890) / 910f, rotation);
    }

    private static void setTextOf12w(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 25 / 650f, pageHeight * (910f - 382) / 910f, rotation);
//        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 530 / 650f, pageHeight * (910f - 258) / 910f, rotation);

        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 90 / 650f, pageHeight * (910f - 705) / 910f, rotation);
        under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 282 / 650f, pageHeight * (910f - 890) / 910f, rotation);
    }

    private static void setSimpleText(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        int length = text.length();
        if (length < 9) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 130 / 682f, pageHeight * (967f - 345) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 315 / 682f, pageHeight * (967f - 785) / 967f, rotation);
        } else if (length == 9) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 80 / 682f, pageHeight * (967f - 380) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 300 / 682f, pageHeight * (967f - 800) / 967f, rotation);
        } else if (length == 10) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 60 / 682f, pageHeight * (967f - 420) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 290 / 682f, pageHeight * (967f - 800) / 967f, rotation);
        } else if (length == 11) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 45 / 682f, pageHeight * (967f - 450) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 270 / 682f, pageHeight * (967f - 830) / 967f, rotation);
        } else if (length == 12) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 35 / 682f, pageHeight * (967f - 475) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 240 / 682f, pageHeight * (967f - 860) / 967f, rotation);
        } else if (length == 13) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 30 / 682f, pageHeight * (967f - 510) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 205 / 682f, pageHeight * (967f - 890) / 967f, rotation);
        } else {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 30 / 682f, pageHeight * (967f - 510) / 967f, rotation);
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 205 / 682f, pageHeight * (967f - 890) / 967f, rotation);
        }
    }

    private static void setTextOfLowerRight(String text, PdfContentByte under, float pageHeight, float pageWidth, float rotation) {
        // 从上到下，从左到右
        int length = text.length();
        if (length < 8) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 395 / 682f, pageHeight * (967f - 880) / 967f, rotation);
        } else if (length == 8) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 375 / 682f, pageHeight * (967f - 900) / 967f, rotation);
        } else if (length == 9) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 355 / 682f, pageHeight * (967f - 920) / 967f, rotation);
        } else if (length == 10) {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 335 / 682f, pageHeight * (967f - 940) / 967f, rotation);
        } else {
            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 315 / 682f, pageHeight * (967f - 960) / 967f, rotation);
        }
//        else if (length == 12) {
//            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 295 / 682f, pageHeight * (967f - 960) / 967f, rotation);
//        } else {
//            under.showTextAligned(Element.ALIGN_LEFT, text, pageWidth * 275 / 682f, pageHeight * (967f - 960) / 967f, rotation);
//        }

    }

    public static void main(String[] args) {
        /*添加文字水印*/
        String inputFilePath = "D:\\file\\temp.pdf";
        Integer type = WatermarkEnum.SIMPLE.getCode();
        // 设置填充操作的不透明度，0.0f表示完全透明，1.0f表示完全不透明，默认不透明度是1.0f
        Float opacity = 0.5f;
        Integer fontColor = WatermarkFontEnum.RED.getCode();
        try {
//            addWatermark(filePath1, "", "你好，水印");

//            waterMark(inputFilePath, "D:\\file\\4words.pdf", "不忘初心", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\5words.pdf", "离离原上草", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\6words.pdf", "刻苦勤奋努力", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\7words.pdf", "秦时明月汉时关", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\8words.pdf", "好好学习天天向上", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\9words.pdf", "窦燕山有义方子不学", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\10words.pdf", "明月几时有把酒问青天", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\11words.pdf", "不知天上宫阙今夕是何年", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\12words.pdf", "我欲乘风归去又恐琼楼玉宇", type, opacity, fontColor);
//            waterMark(inputFilePath, "D:\\file\\13words.pdf", "无所谓别说我的眼泪你无所谓", type, opacity, fontColor);
            waterMark(inputFilePath, "D:\\file\\14words.pdf", "葡萄美酒夜光杯欲饮琵琶马上催", type, opacity, fontColor);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
